﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SchoolManagementSystem
{
    public partial class KategoriKeuangan : Form
    {
        public KategoriKeuangan()
        {
            InitializeComponent();
        }

        private void KategoriKeuangan_Load(object sender, EventArgs e)
        {
            refreshTreeview();
        }

        private void refreshTreeview()
        {
            trvKategori.Nodes.Clear();

            // Initialize node
            TreeNode node1 = new TreeNode("Pengeluaran");
            TreeNode node2 = new TreeNode("Pemasukan");
            node1.Name = "0-0";
            node2.Name = "1-0";
            trvKategori.Nodes.Add(node1);
            trvKategori.Nodes.Add(node2);

            // Read expense types/categories
            DataTable dt = Database.getInstance().query("SELECT * FROM Expense_type ORDER BY id");
            //for(int i = dt.Rows.Count - 1; i >= 0; i--)
            foreach(DataRow row in dt.Rows)
            {
                // DataRow row = dt.Rows[i];
                TreeNode node = new TreeNode(row[1].ToString());
                node.Name = "0-" + row[0].ToString();
                trvKategori.Nodes[0].Nodes.Add(node);
            }

            // Read income types/categories
            dt = Database.getInstance().query("SELECT * FROM Income_type ORDER BY id");
            //for (int i = dt.Rows.Count - 1; i >= 0; i--)
            foreach(DataRow row in dt.Rows)
            {
                //DataRow row = dt.Rows[i];
                TreeNode node = new TreeNode(row[1].ToString());
                node.Name = "1-" + row[0].ToString();
                trvKategori.Nodes[1].Nodes.Add(node);
            }

            // Expand nodes
            trvKategori.ExpandAll();
        }

        private void btnTambah_Click(object sender, EventArgs e)
        {
            if (txtNama.Text.Length == 0 || cmbTipe.SelectedIndex < 0)
            {
                MessageBox.Show("Mohon lengkapi input data", "Peringatan", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }

            try
            {
                string sql;
                if (cmbTipe.SelectedIndex == 0) sql = "INSERT INTO Expense_type([type]) VALUES('" + txtNama.Text + "')";
                else sql = "INSERT INTO Income_type([type]) VALUES('" + txtNama.Text + "')";

                Database.getInstance().nonquery(sql);
            }
            catch (Exception ex)
            {
                string msg = ex.Message;
                if(msg.Contains("UNIQUE KEY")) msg = "Sudah terdapat kategori yang sama, kategori tidak dapat ditambahkan";
                MessageBox.Show(msg,"Gagal",MessageBoxButtons.OK,MessageBoxIcon.Error);
                return;
            }

            refreshTreeview();
        }

        private void btnHapus_Click(object sender, EventArgs e)
        {
            TreeNode node = trvKategori.SelectedNode;
            string[] ids = node.Name.Split(new char[] { '-' });

            // if root node or first node, return
            if (ids[1] == "0" || ids[1] == "1")
            {
                MessageBox.Show("Kategori ini tidak bisa dihapus", "Peringatan", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }

            DialogResult ans = MessageBox.Show("Hapus kategori ini?\nSemua transaksi dengan kategori ini tidak akan muncul dalam keuangan", "Peringatan", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
            if (ans == DialogResult.Cancel) return;

            // Delete category
            if (ids[0] == "0") deleteCategory(Category.PENGELUARAN, ids[1]);
            else deleteCategory(Category.PEMASUKAN, ids[1]);

            refreshTreeview();
        }

        private enum Category {PENGELUARAN, PEMASUKAN}
        private void deleteCategory(Category category, string cat_id)
        {
            string sql;
            if (category == Category.PEMASUKAN)
                sql = "DELETE FROM Income_type WHERE id = " + cat_id;
            else
                sql = "DELETE FROM Expense_type WHERE id = " + cat_id;

            Database.getInstance().nonquery(sql);
        }
    }
}
